﻿using System.Collections.Generic;

namespace Framework
{
    public class SLObjectPool<T> where T : SLICacheable, new()
    {
        private readonly Queue<T> _cache = new Queue<T>();
        private readonly int _maxCount = 32;

        public SLObjectPool(int max)
        {
            _maxCount = max;
        }

        public T GetObject() => CurCount > 0 ? _cache.Dequeue() : new T();

        public void PutObject(T obj)
        {
            if (null == obj)
            {
                return;
            }
            obj.Reset();
            if (_cache.Count < _maxCount)
            {
                _cache.Enqueue(obj);
            }
        }
        public int CurCount => _cache.Count;
        public void Clear()
        {
            _cache.Clear();
        }
    }
}
